package org.swingplus.core.factory.support;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultSingletonBeanRegistry {
	
	/** Cache of singleton objects: bean name to bean instance. */
	private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

	/** Cache of singleton factories: bean name to ObjectFactory. */
//	private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16);

	/** Cache of early singleton objects: bean name to bean instance. */
	private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);

	/** Set of registered singletons, containing the bean names in registration order. */
	private final Set<String> registeredSingletons = new LinkedHashSet<>(256);

	/** Names of beans that are currently in creation. */
	private final Set<String> singletonsCurrentlyInCreation =
			Collections.newSetFromMap(new ConcurrentHashMap<>(16));

	/** Names of beans currently excluded from in creation checks. */
	private final Set<String> inCreationCheckExclusions = Collections.newSetFromMap(new ConcurrentHashMap<>(16));

	/**
	 * 该bean是否创建中
	 * @param beanName
	 * @return
	 */
	public boolean isSingletonCurrentlyInCreation(String beanName) {
		
		return this.singletonsCurrentlyInCreation.contains(beanName);
		
	}
	/**
	 * 该bean是否创建中
	 * @param beanName
	 * @return
	 */
	public boolean setSingletonCurrentlyInCreation(String beanName) {

		return this.singletonsCurrentlyInCreation.add(beanName);

	}
    /**
     * 该bean是否创建中
     * @param beanName
     * @return
     */
    public boolean removeSingletonCurrentlyInCreation(String beanName) {
        return this.singletonsCurrentlyInCreation.remove(beanName);
    }

	public void removeEarlySingletonObjects(String beanName) {
		this.earlySingletonObjects.remove(beanName);
	}
	public void setEarlySingletonObjects(String beanName,Object object) {
		 this.earlySingletonObjects.put(beanName,object);
	}


	public Object getSingletion(String beanName){
		return this.singletonObjects.get(beanName);

	}


	public void addSingletonObjects(String beanName,Object object){
		this.singletonObjects.put(beanName,object);
	}

    public Object getEarlySingletonObjects(String beanName){
       return  this.earlySingletonObjects.get(beanName);
    }


}
